import {bCreate, created, bMount, mounted, bUpdate, updated, bDestroy, destroy} from "./types";
import lifecycles from "@/lifecycle/handle/main";
import {$options} from "@/instance";

export const initT = 'initT', bUpdateT = 'bUpdateT', updatedT = 'updatedT', bDestroyT = 'bDestroyT',
  destroyT = 'destroyT';
export const handleLifecycle = (vm, actionType) => {
  let lifecycleHooks;

  if (actionType === initT) {
    const {el, template} = vm[$options];
    // if has no `el` or `template`, will callback to beforeMount.
    if (el || template) {
      lifecycleHooks = [bCreate, created, bMount, mounted];
    } else {
      lifecycleHooks = [bCreate, created, bMount]
    }
  }

  if (actionType === bUpdateT) {
    lifecycleHooks = [bUpdate];
  }

  if (actionType === updatedT) {
    lifecycleHooks = [updated];
  }

  if (actionType === bDestroyT) {
    lifecycleHooks = [bDestroy];
  }


  if (actionType === destroyT) {
    lifecycleHooks = [destroy];
  }


  for (let hookName of lifecycleHooks) {
    // You should do something in the hooks.
    const mineHook = lifecycles[hookName];
    typeof mineHook === 'function' && mineHook(vm);
    const customHook = vm[$options][hookName];
    // ... with customer hookName， should handle the `this` instance.
    typeof customHook === 'function' && customHook.bind(vm)();
  }
}
